package com.zhongwang.business.module.invoiceManage.controller;

import com.github.pagehelper.PageHelper;
import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.gen.entity.User;
import com.zhongwang.business.module.fileManage.model.FileModel;
import com.zhongwang.business.module.invoiceManage.model.InvoiceListModel;
import com.zhongwang.business.module.invoiceManage.service.InvoiceListService;
import com.zhongwang.business.util.ShiroUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.zhongwang.business.common.JsonResult.ResultType.ERROR;
import static com.zhongwang.business.common.JsonResult.ResultType.SUCCESS;

@Controller
public class InvoiceListController extends BaseController {

    @Autowired
    private InvoiceListService invoiceListService;


    /**
     * 首页跳转到款票列表页
     *
     * @param model
     * @return
     */
    @GetMapping("/invoiceList/listIndex")
    @RequiresPermissions("pay:list:view")
    public String linkInvoiceList(Model model) {
        int loginUid = ShiroUtils.getUserId();
        String loginUAccount = ShiroUtils.getUser().getAccount();
        String loginUName = ShiroUtils.getUser().getName();
        model.addAttribute("loginUid", loginUid);
        model.addAttribute("loginUAccount", loginUAccount);
        model.addAttribute("loginUName", loginUName);
        int orderNo = invoiceListService.selectUserLevel(loginUid);
        model.addAttribute("orderNo", orderNo);
        return "/invoiceManage/invoiceList/listIndex";
    }

    /**
     * 首页跳转到款票审核
     *
     * @param model
     * @return
     */
    @GetMapping("/invoiceList/listIndex2")
    @RequiresPermissions("pay:audit:view")
    public String linkInvoiceList2(Model model) {
        int loginUid = ShiroUtils.getUserId();
        String loginUAccount = ShiroUtils.getUser().getAccount();
        String loginUName = ShiroUtils.getUser().getName();
        model.addAttribute("loginUid", loginUid);
        model.addAttribute("loginUAccount", loginUAccount);
        model.addAttribute("loginUName", loginUName);
        return "/invoiceManage/invoiceList/listIndex2";
    }

    /**
     * 首页跳转到提交页
     *
     * @param model
     * @return
     */
    @GetMapping("/invoiceList/listIndex3")
    @RequiresPermissions("pay:com:view")
    public String linkInvoiceList3(Model model) {
        int loginUid = ShiroUtils.getUserId();
        String loginUAccount = ShiroUtils.getUser().getAccount();
        String loginUName = ShiroUtils.getUser().getName();
        model.addAttribute("loginUid", loginUid);
        model.addAttribute("loginUAccount", loginUAccount);
        model.addAttribute("loginUName", loginUName);

        return "/invoiceManage/invoiceList/listIndex3";

    }

    /**
     * 首页跳转到查看页
     *
     * @param model
     * @return
     */
    @GetMapping("/invoiceList/listIndex4")
    @RequiresPermissions("pay:inqList:view")
    public String linkInvoiceList4(Model model) {
        int loginUid = ShiroUtils.getUserId();
        String loginUAccount = ShiroUtils.getUser().getAccount();
        String loginUName = ShiroUtils.getUser().getName();
        model.addAttribute("loginUid", loginUid);
        model.addAttribute("loginUAccount", loginUAccount);
        model.addAttribute("loginUName", loginUName);
        return "/invoiceManage/invoiceList/listIndex4";
    }

    @GetMapping("/invoiceList/xiaoshou1")
    @RequiresPermissions("pay:inqList:view")
    public String xiaoshou1(Model model) {
        return "/invoiceManage/invoiceList/xiaoshou1";
    }
    @GetMapping("/invoiceList/xiaoshou2")
    @RequiresPermissions("pay:inqList:view")
    public String xiaoshou2(Model model) {
        return "/invoiceManage/invoiceList/xiaoshou2";
    }
    /**
     * 跳转到编辑页
     *
     * @param model
     * @return
     */
    @GetMapping("/invoiceList/kpEdit")
    public String linkKpEdit(Model model) {
        int loginUid = ShiroUtils.getUserId();
        String loginUAccount = ShiroUtils.getUser().getAccount();
        String loginUName = ShiroUtils.getUser().getName();
        model.addAttribute("loginUid", loginUid);
        model.addAttribute("loginUAccount", loginUAccount);
        model.addAttribute("loginUName", loginUName);

        return "invoiceManage/invoiceList/kpSelect";
    }


    /**
     * 获取 款票列表  赵晨博
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/InvoiceList")
    public JsonResult selectAllInvoiceList(InvoiceListModel model) {
//        startPage();
        return jsonResult(invoiceListService.invoiceListModelList(model));
    }


    /**
     * 获取 款票查询  赵晨博
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/InvoiceList4")
    public JsonResult selectAllInvoiceList4(InvoiceListModel model) {
//        startPage();
        return jsonResult(invoiceListService.invoiceListModelList4(model));
    }


    /**
     * 获取  审批过程信息  赵晨博
     *
     * @param conId
     * @return
     */
    @ResponseBody
    @PostMapping("/approveProcess")
    public JsonResult selectAllInvoiceList(int conId) {
//        startPage();
        return jsonResult(invoiceListService.selectAppProcessByConId(conId));
    }


    /**
     * 获取 款票审核列表  赵晨博
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/InvoiceList2")
    public JsonResult selectAllInvoiceList2(InvoiceListModel model) {
//        startPage();
        User user = ShiroUtils.getUser();
        model.setLoginUid(user.getId());
        return jsonResult(invoiceListService.invoiceListModelList2(model));
    }

    /**
     * 获取 款票提交列表  赵晨博
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/InvoiceList3")
    public JsonResult selectAllInvoiceList3(InvoiceListModel model) {
//        startPage();
        User user = ShiroUtils.getUser();
        model.setLoginUName(user.getName());
        return jsonResult(invoiceListService.invoiceListModelList3(model));
    }

    /**
     * 跳转至编辑页面
     *
     * @param conId 合同id
     * @param model
     * @return
     */
    @GetMapping("/kpEdit/contractInfo")
    @RequiresPermissions(value = {"pay:infor:view", "pay:com:edit"}, logical = Logical.OR)
    public String viewPage1(@RequestParam("conId") Integer conId, Model model) {
        InvoiceListModel info = invoiceListService.contractInfo(conId);
        model.addAttribute("info", info);
        int loginUid = ShiroUtils.getUserId();
        String loginUAccount = ShiroUtils.getUser().getAccount();
        String loginUName = ShiroUtils.getUser().getName();
        model.addAttribute("loginUid", loginUid);
        model.addAttribute("loginUAccount", loginUAccount);
        model.addAttribute("loginUName", loginUName);
        return "invoiceManage/invoiceList/kpEdit";
    }

    /**
     * 跳转至审核页面
     *
     * @param conId 合同id
     * @param model
     * @return
     */
    @GetMapping("/kpExamine/contractInfo")
    @RequiresPermissions("pay:audit:view")
    public String viewPage3(@RequestParam("conId") Integer conId,
                            @RequestParam("processId") Integer processId,
                            Model model) {
        InvoiceListModel info = invoiceListService.contractInfo(conId);
        model.addAttribute("info", info);
        model.addAttribute("processId", processId);
        model.addAttribute("conId", conId);

        List<InvoiceListModel> appList = invoiceListService.selAppList(conId);
        if (appList != null) {
            if (appList.size() > 0) {
                if (!appList.get(0).getProjectName().equals("驳回")) {
                    appList = new ArrayList<>();
                }
            }
        }
        model.addAttribute("appList", appList);
        return "invoiceManage/invoiceList/kpExamine";
    }

    /**
     * 跳转至查看页面
     *
     * @param conId 合同id
     * @param model
     * @return
     */
    @GetMapping("/kpSelect/contractInfo")
//    @RequiresPermissions("pay:audit:view")
    public String viewPage2(@RequestParam("conId") Integer conId, Model model) {
        InvoiceListModel info = invoiceListService.contractInfo(conId);
        model.addAttribute("info", info);
        int loginUid = ShiroUtils.getUserId();
        String loginUAccount = ShiroUtils.getUser().getAccount();
        String loginUName = ShiroUtils.getUser().getName();
        model.addAttribute("loginUid", loginUid);
        model.addAttribute("loginUAccount", loginUAccount);
        model.addAttribute("loginUName", loginUName);
        List<InvoiceListModel> appList = invoiceListService.selAppList(conId);
        if (appList != null) {
            if (appList.size() > 0) {
                if (!appList.get(0).getProjectName().equals("驳回")) {
                    appList = new ArrayList<>();
                }
            }
        }
        model.addAttribute("appList", appList);
        return "invoiceManage/invoiceList/kpSelect";
    }


    /**
     * 修改款票状态 为 保存未提交 1
     *
     * @param conId
     * @return
     */
    @ResponseBody
    @GetMapping("/kpSave")
    public JsonResult kpSaveByConId(Integer conId) {
        JsonResult jsonResult = new JsonResult();
        int i = invoiceListService.updateKpStatus(conId);
        if (i == 1) {
            jsonResult.setMsg("保存成功");
            jsonResult.setType(SUCCESS);
            return jsonResult;
        } else {
            jsonResult.setMsg("保存失败");
            jsonResult.setType(ERROR);
            return jsonResult;
        }
    }


    /**
     * 回款信息 by conid
     *
     * @param conId
     * @return
     */
    @ResponseBody
    @PostMapping("/moneyInfoByConId")
    public JsonResult selectMoneyInfoByConId(Integer conId) {
        startPage();
        return jsonResult(invoiceListService.returnMoneyInfoByConId(conId));
    }

    /**
     * 总包回款信息 by conid
     *
     * @param conId
     * @return
     */
    @ResponseBody
    @PostMapping("/ZBmoneyInfoByConId")
    public JsonResult selectZBMoneyInfoByConId(Integer conId) {
        return jsonResult(invoiceListService.returnZBMoneyInfoByConId(conId));
    }

    /**
     * 回款信息 by conIdAndRmiMoneyType
     *
     * @param conId rmiMoneyType
     * @return
     */
    @ResponseBody
    @PostMapping("/moneyInfoByConIdAndRmiMoneyType")
    public InvoiceListModel selectMoneyInfoByConIdAndRmiMoneyType(Integer conId, Integer rmiMoneyType) {
        InvoiceListModel model = invoiceListService.rmInfoByConIdAndRmiMoneyType(conId, rmiMoneyType);
        return model;
    }


    /**
     * 新 修改回款信息 （非总包）
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/saveUpdateMoney")
    @RequiresPermissions("pay:infor:edit")
    public JsonResult saveUpdateMoney(InvoiceListModel model) {
        JsonResult jsonResult = new JsonResult();
        int flag = invoiceListService.saveUpdateMoney(model);
        if (flag > 0) {
            jsonResult.setType(SUCCESS);
            jsonResult.setMsg("修改成功");
        } else {
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("修改失败");
        }
        return jsonResult;
    }

    /**
     * 根据合同id查询发票列表
     *
     * @param conId
     * @return
     */
    @ResponseBody
    @PostMapping("/invInfoByConId")
    public JsonResult invInfoByConId(Integer conId) {
//        startPage();
        List<InvoiceListModel> model = invoiceListService.InvInfoByConId(conId);
        return jsonResult(model);


    }


    /**
     * 上传附件
     *
     * @param file
     * @return
     */
    @ResponseBody
    @PostMapping("/invoice/uploadFile")
    @RequiresPermissions({"pay:infor:upload"})
    public JsonResult uploadFile(@RequestParam("file") MultipartFile[] file,
                                 @RequestParam("invId") Integer invId,
                                 @RequestParam("remark") String remark
    ) {
        JsonResult jsonResult = new JsonResult();
        try {
            int flg = invoiceListService.uploadFile(file, invId, remark);
            if (flg > 0) {
                jsonResult.setType(SUCCESS);
                jsonResult.setData(flg);
                jsonResult.setMsg("上传成功");
            }
        } catch (Exception ex) {
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setData(invId);
            jsonResult.setMsg("上传失败：" + ex.getMessage());
        }
        return jsonResult;
    }


    /**
     * 根据发票id查询附件列表
     *
     * @param invId
     * @return
     */
    @ResponseBody
    @PostMapping("/annexListByInvId")
    public JsonResult annexListByInvId(Integer invId) {
        List<FileModel> model = invoiceListService.FileList(invId);
        return jsonResult(model);
    }


    /**
     * 下载附件
     *
     * @param fileId
     * @param response
     * @return
     */
    @ResponseBody
    @GetMapping("/annexDownLoad")
    public String annexDownLoad(Integer fileId, HttpServletResponse response) {
        String result = invoiceListService.DownLoad(fileId, response);
        return result;
    }

    /**
     * 删除附件
     *
     * @param fileId
     * @return
     */
    @ResponseBody
    @PostMapping("/annexDelete")
    @RequiresPermissions("pay:infor:delete")
    public String annexDelete(Integer fileId, String invCode) {
        String result = invoiceListService.deleteAnnexByFileId(fileId, invCode);
        return result;
    }

    /**
     * 删除发票
     *
     * @param invId
     * @return
     */
    @ResponseBody
    @GetMapping("/invoiceDelete")
    @RequiresPermissions("pay:infor:del")
    public String invoiceDelete(Integer invId) {
        String result = invoiceListService.deleteInvoiceByInvId(invId);
        return result;
    }


    /**
     * 添加发票
     *
     * @param
     * @return
     */
    @ResponseBody
    @PostMapping("/invoiceAdd")
    public JsonResult invoiceAdd(Integer invConId,
                                 String invCode,
                                 String invGoodsName,
                                 BigDecimal invAmount,
                                 Date invDate,
                                 MultipartFile[] file,
                                 String remark) {
        JsonResult jsonResult = new JsonResult();
        if (invCode != null && invGoodsName != null && invAmount != null && invDate != null) {
            String msg = invoiceListService.addInvoice(invConId, invCode, invGoodsName, invAmount, invDate, file, remark);
            jsonResult.setType(SUCCESS);
            jsonResult.setMsg(msg);
            return jsonResult;
        } else {
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("添加失败");
            return jsonResult;
        }
    }


    /**
     * @Description:invoiceUpdate 修改发票
     * @Author: zcb
     * @Date: 2020-12-05 10:24:05
     * @Param:[invConId, invId, invCode, invGoodsName, invAmount, invDate, file, remark]
     * @Return:com.zhongwang.business.common.JsonResult
     **/
    @ResponseBody
    @PostMapping("/invoiceUpdate")
    @RequiresPermissions("pay:infor:edit")
    public JsonResult invoiceUpdate(Integer invConId,
                                    Integer invId,
                                    String invCode,
                                    String invGoodsName,
                                    BigDecimal invAmount,
                                    Date invDate,
                                    MultipartFile[] file,
                                    String remark) {
        JsonResult jsonResult = new JsonResult();
        if (invCode != null && invGoodsName != null && invAmount != null && invDate != null) {
            String msg = invoiceListService.InvoiceUpdate(invConId, invId, invCode, invGoodsName, invAmount, invDate, file, remark);
            if (msg == "修改成功" || msg.equals("修改成功")) {
                jsonResult.setType(SUCCESS);
                jsonResult.setMsg(msg);
                return jsonResult;
            } else {
                jsonResult.setType(JsonResult.ResultType.ERROR);
                jsonResult.setMsg("修改失败");
                return jsonResult;
            }
        } else {
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("请确认数据");
            return jsonResult;
        }
    }

    /**
     * 款票提交
     */
    @ResponseBody
    @PostMapping("/invoiceManage/kpAppInfo")
    @RequiresPermissions("pay:com:commit")
    public JsonResult kpAppInfo(@RequestParam Integer conId) {
        JsonResult jsonResult = new JsonResult();
        int a = invoiceListService.kpAppInfo(conId);
        if (a > 0) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("提交成功");
        } else {
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("失败");
        }
        return jsonResult;
    }


    /**
     * 款票审核
     */
    @ResponseBody
    @PostMapping("/invoiceManage/kpAppInfoByUserRole")
    @RequiresPermissions("pay:audit:check")
    public JsonResult kpAppInfoByUserRole(InvoiceListModel model) {
        JsonResult jsonResult = new JsonResult();
        int a = 1;
        a = invoiceListService.approveKpInfoByBllId(model);
        if (a > 0) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("审核成功");
        } else {
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("失败");
        }
        return jsonResult;
    }



    @ResponseBody
    @GetMapping("inputTest")
    public List<String> input(String sellCode) {
        List<InvoiceListModel> model = invoiceListService.selectLikeTest(sellCode);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < model.size(); i++) {
            String A = model.get(i).getSellCode();
            list.add(model.get(i).getSellCode());
        }
        return list;
    }


}



